﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace SmapleWinformApplication
{    
    public partial class Form1 : Form
    {
        int EMPID = 83943;
        List<string> MonthsCollection = new List<string>();
        sampleDBEntities entities = new sampleDBEntities();
        HomeScreen form;
        public Form1(HomeScreen _form)
        {
            InitializeComponent();
            this.form = _form;
            form.toolStrip1.Enabled = true;
            InitialData();            
        }

        public void InitialData()
        {
           // CbxType.SelectedIndex = 0;

            MonthsCollection.Add("Jan");
            MonthsCollection.Add("Feb");
            MonthsCollection.Add("Mar");
            MonthsCollection.Add("Apr");
            MonthsCollection.Add("May");
            MonthsCollection.Add("Jun");
            MonthsCollection.Add("Jul");
            MonthsCollection.Add("Aug");
            MonthsCollection.Add("Sep");
            MonthsCollection.Add("Oct");
            MonthsCollection.Add("Nov");
            MonthsCollection.Add("Dec");          

            lblName.Text = "P. ASHOK KUMAR";
            lblPRNO.Text = EMPID.ToString();

            CalculateTotals();
            FetchDataAndDisplay();
        }       

        //public void ClearEntry()
        //{            
        //    CbxType.SelectedIndex = 0;
        //    checkBox1.Checked = false;
        //}

        public void CalculateTotals()
        {
            decimal ELCount = 0, MLCount = 0, SPLCount = 0, LOPCount = 0;

            var query = from d in entities.TIME_TRACKER
                        where d.EMPID == EMPID && d.STATUS != "Delete"
                        select d;

            foreach (var item in query)
            {
                if (item.TYPE == "EL")
                {
                    ELCount = ELCount + Convert.ToDecimal(item.UNIT);
                }
                else if (item.TYPE == "ML")
                {
                    MLCount = MLCount + Convert.ToDecimal(item.UNIT);
                }
                else if (item.TYPE == "SPL")
                {
                    SPLCount = SPLCount + Convert.ToDecimal(item.UNIT);
                }
                else if (item.TYPE == "LOP")
                {
                    LOPCount = LOPCount + Convert.ToDecimal(item.UNIT);
                }
            }

            lblEL.Text = ELCount.ToString();
            lblML.Text = MLCount.ToString();
            lblLOP.Text = LOPCount.ToString();
            lblSPL.Text = SPLCount.ToString();
        }

        public void DoEntryToTable(int period)
        {
            //decimal TempPeriod = 0, ELCount=0,MLCount=0,SPLCount=0;
            
            //var query = from d in entities.TIME_TRACKER 
            //            where d.EMPID == EMPID
            //            select d;

            //foreach (var item in query)
            //{
            //    if(item.TYPE == "EL")
            //         ELCount = ELCount + Convert.ToDecimal(item.UNIT);
            //    else if (item.TYPE == "ML")
            //         MLCount = MLCount + Convert.ToDecimal(item.UNIT);
            //    else if (item.TYPE == "SPL")
            //        SPLCount = SPLCount + Convert.ToDecimal(item.UNIT);
            //}            
           
            //int count = query.Count();
            //TIME_TRACKER tracker = new TIME_TRACKER();
            //tracker.INDEX = count + 1; 
            //tracker.EMPID = 83943;
            //tracker.YEAR = DateTime.Now.Year;
            //DateTime date = DateTime.Now;
            //tracker.MONTH = date.ToString("MMM");

            //if (CbxType.SelectedItem.ToString() == "ML" && MLCount < 10)
            //{
            //    tracker.TYPE = "ML";
            //}
            //else if (CbxType.SelectedItem.ToString() == "SPL" && SPLCount < 90)
            //{
            //    tracker.TYPE = "SPL";
            //}

            //else if (CbxType.SelectedItem.ToString() == "EL" && ELCount < 30)
            //{
            //    tracker.TYPE = "EL";
            //}
            //else
            //{
            //    tracker.TYPE = "LOP";
            //}
            //if (checkBox1.Checked)
            //{
            //    tracker.UNIT = Convert.ToDecimal(0.5) ;
            //}
            //else
            //{
            //    tracker.UNIT = Convert.ToDecimal(-(period - 1));
            //}
            //tracker.FROMDATE = dateTimePicker1.Value;
            //tracker.TODATE = dateTimePicker2.Value;

            //entities.AddToTIME_TRACKER(tracker);
            //entities.SaveChanges();

            //FetchDataAndDisplay();
            //CalculateTotals();
            //ClearEntry();
        }

        public void FetchDataAndDisplay()
        {
            decimal ELPeriod = 0, MLPeriod = 0, SPLPeriod = 0, LOPPeriod=0;
            dataGridView1.Rows.Clear();
            
            foreach (var month in MonthsCollection)
            {
                var query = from d in entities.TIME_TRACKER
                            where d.MONTH == month && d.STATUS != "Delete"
                            select d;

                foreach (var item in query)
                {
                    if(item.TYPE=="EL")
                        ELPeriod = ELPeriod + Convert.ToDecimal(item.UNIT);
                    else if (item.TYPE=="ML")
                        MLPeriod = MLPeriod + Convert.ToDecimal(item.UNIT);
                    else if (item.TYPE =="SPL")
                        SPLPeriod = SPLPeriod + Convert.ToDecimal(item.UNIT);
                    else if (item.TYPE == "LOP")
                        LOPPeriod = LOPPeriod + Convert.ToDecimal(item.UNIT);
                }

                dataGridView1.Rows.Add(month, ELPeriod.ToString(), MLPeriod.ToString(),LOPPeriod.ToString(), SPLPeriod.ToString());
                SPLPeriod = 0;
                ELPeriod = 0;
                MLPeriod = 0;
                LOPPeriod = 0;
            }
        }

        private void BtnAdd_Click_1(object sender, EventArgs e)
        {
            //if (CbxType.SelectedIndex > 0)
            //{
            //    DateTime dateFrom = dateTimePicker1.Value;
            //    DateTime dateTo = dateTimePicker2.Value;
            //    TimeSpan spanDiff = dateFrom - dateTo;
            //    int dateDiff = Convert.ToInt32(spanDiff.TotalDays);


            //    DoEntryToTable(dateDiff);
            //    CalculateTotals();
            //    ClearEntry();
            //}
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //if (checkBox1.Checked==true)
            //{
            //    dateTimePicker1.Visible = false;
            //    dateTimePicker2.Visible = false;
            //}
            //else
            //{
            //    dateTimePicker1.Visible = true;
            //    dateTimePicker2.Visible = true;
            //}
        }              

        private void lblEL_TextChanged(object sender, EventArgs e)
        {
            double value = Convert.ToDouble(lblEL.Text);
            if (value > 25)
                lblEL.ForeColor = Color.Red;
            else if (value > 15 && value < 25)
                lblEL.ForeColor = Color.Yellow;
        }

        private void lblSPL_TextChanged(object sender, EventArgs e)
        {
            double value = Convert.ToDouble(lblSPL.Text);
            if (value > 85)
                lblSPL.ForeColor = Color.Red;
            else if (value > 60 && value < 85)
                lblSPL.ForeColor = Color.Yellow;
        }

        private void lblML_TextChanged(object sender, EventArgs e)
        {
            double value = Convert.ToDouble(lblML.Text);
            if (value > 8)
                lblML.ForeColor = Color.Red;
            else if (value > 5 && value < 8)
                lblML.ForeColor = Color.Yellow;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            string MonthCode = null;
            if (e.ColumnIndex != 0)
            {
                string Month = dataGridView1.Columns[e.ColumnIndex].HeaderText.ToString();

                if (Month == "Earned Leave")
                    MonthCode = "EL";
                else if (Month == "Medical Leave")
                    MonthCode = "ML";
                else if (Month == "Special Leave")
                    MonthCode = "SPL";
                else if (Month == "Loss Of Pay")
                    MonthCode = "LOP";

                Form2 form2 = new Form2(form, MonthCode, dataGridView1[0, e.RowIndex].Value.ToString(), 2013);
                form2.MdiParent = form;
                this.Close();
                form2.Show();
            }
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }       
    }
}
